package com.dream.keigezhushou.Activity.kege.adapter;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.StringUtils;
import com.dream.keigezhushou.Activity.kege.acty.RemoteMusicActivity;
import com.dream.keigezhushou.Activity.kege.acty.SingingDetailActivity;
import com.dream.keigezhushou.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/3/17.
 * 我的点歌
 */
public class RemoteMusicAdapter extends BaseAdapter {

    private Context mContext;
    boolean result = false;
    boolean isOpen = false;
    private OnItemClickLinstener onitemClickLinstener = null;
    private ArrayList<String> dateList;
    int downX = 0;
    int dx;
    float ddx;
    int bottomWidth;
    ProHolder mHolder = null;

    public RemoteMusicAdapter(Context context, ArrayList<String> dateList) {
        this.mContext = context;
        this.dateList = dateList;
    }

    public void setOnItemClickLinstener(OnItemClickLinstener onitemClickLinstener) {
        this.onitemClickLinstener = onitemClickLinstener;
    }


    @Override
    public int getCount() {
        return dateList.size();
    }

    @Override
    public Object getItem(int position) {
        return dateList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_remote_music, null);
            mHolder = new ProHolder();

            mHolder.conversatinListview_front = (LinearLayout) view.findViewById(R.id.conversatinListview_front);
            mHolder.btn_detete = (Button) view.findViewById(R.id.conversationlist_delete);
            mHolder.nameTexView = (TextView) view.findViewById(R.id.item_remote_name);
            mHolder.singStateTv = (TextView) view.findViewById(R.id.my_singing_join);
            view.setTag(mHolder);
        } else {
            mHolder = (ProHolder) view.getTag();
        }
        mHolder.conversatinListview_front.setTag(mHolder);
        mHolder.singStateTv.setText(dateList.get(i));
        mHolder.btn_detete.setTag(mHolder);
//        mHolder.postion = i;

        //K歌或者合唱按钮点击事件
        mHolder.singStateTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onitemClickLinstener!=null){
                    onitemClickLinstener.onSingStateClick(i,dateList.get(i));
                }


            }
        });


        //删除按钮点击事件
        mHolder.btn_detete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ProHolder holder = (ProHolder) v.getTag();

                if (onitemClickLinstener != null) {

                    onitemClickLinstener.onDetelClick(i);

                }

            }
        });

        //点击item


        mHolder.conversatinListview_front.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



                    Intent intent = new Intent();
                    intent.putExtra(StringUtils.KEGESTATE, dateList.get(i));
                    intent.setClass(mContext, SingingDetailActivity.class);
                    mContext.startActivity(intent);



            }
        });





        //侧滑触摸事件
        mHolder.conversatinListview_front.setOnTouchListener(new View.OnTouchListener() {
            private Point pointDownPoint;
            private Point pointUpPoint;
            private boolean isdelete;


            @Override
            public boolean onTouch(View v, MotionEvent event) {




                ProHolder holder = (ProHolder) v.getTag();
                bottomWidth = holder.btn_detete.getWidth();




                switch (event.getAction()) {


                    case MotionEvent.ACTION_DOWN:
                        // Log.i("", "ACTION_DOWN");
                        downX = (int) event.getRawX();
                        result = true;

                        break;
                    case MotionEvent.ACTION_MOVE:
                        dx = (int) (event.getRawX() - downX);
                        if (isOpen) {
                            // 打开状态
                            // 向右滑动
                            if (dx > 0 && dx < bottomWidth) {
                                v.setTranslationX(dx - bottomWidth);
                                // 允许移动，阻止点击
                                result = true;
                            }
                        } else {
                            // 闭合状态
                            // 向左移动
                            if (dx < 0 && Math.abs(dx) < bottomWidth) {
                                v.setTranslationX(dx);
                                // 允许移动，阻止点击
                                result = true;
                            }
                        }
                        break;
                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP:

                        // Log.i("", "ACTION_UP" + v.getTranslationX());

                        // 获取已经移动的
                        ddx = v.getTranslationX();

                        // 判断打开还是关闭

                        if (ddx <= 0 && ddx > -(bottomWidth / 2)) {
                            // 关闭
                            ObjectAnimator oa1 = ObjectAnimator.ofFloat(v, "translationX", ddx, 0).setDuration(100);
                            oa1.start();
                            oa1.addListener(new Animator.AnimatorListener() {
                                @Override
                                public void onAnimationStart(Animator animation) {
                                }

                                @Override
                                public void onAnimationRepeat(Animator animation) {
                                }

                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    isOpen = false;
                                    result = false;
                                }

                                @Override
                                public void onAnimationCancel(Animator animation) {
                                    isOpen = false;
                                    result = false;
                                }
                            });

                        }
                        if (ddx <= -(bottomWidth / 2) && ddx > -bottomWidth) {
                            // 打开
                            ObjectAnimator oa1 = ObjectAnimator.ofFloat(v, "translationX", ddx, -bottomWidth)
                                    .setDuration(100);
                            oa1.start();
                            result = true;
                            isOpen = true;
                        }
                        break;
                }
                return result;
            }
        });


        return view;
    }

    public void removeItem(int position) {
        dateList.remove(position);
    }


    class ProHolder {

        int postion;
        Button btn_detete;
        LinearLayout conversatinListview_front;
        TextView nameTexView;
        TextView singStateTv;
    }


    public interface OnItemClickLinstener {
        public void onItemClick(int postion);

        public void onDetelClick(int postion);

        public void onSingStateClick(int postion,String state);


    }


}